# -*- coding:utf-8 -*-
"""
# @File : myform.py
# @Author:chen
# @Date : 2023/9/17
# @Desc :
"""

from django import forms
from plan import models


class MyregForm(forms.Form):
    username = forms.CharField(label='用户名', min_length=3, max_length=8,
                               error_messages={
                                   'required': '用户名不能为空',
                                   'min_length': '用户名最少3位',
                                   'max_length': '用户名最大8位'

                               },
                               # 还需要让标签有bootstrap样式
                               widget=forms.widgets.TextInput(attrs={'class': 'form-control'})
                               )
    password = forms.CharField(label='密码', min_length=3, max_length=8,
                               error_messages={
                                   'required': '密码不能为空',
                                   'min_length': '密码最少3位',
                                   'max_length': '密码最大8位'

                               },
                               # 还需要让标签有bootstrap样式
                               widget=forms.widgets.PasswordInput(attrs={'class': 'form-control'})
                               )
    confirm_password = forms.CharField(label='确认密码', min_length=3, max_length=8,
                                       error_messages={
                                           'required': '确认密码不能为空',
                                           'min_length': '确认密码最少3位',
                                           'max_length': '确认密码最大8位'

                                       },
                                       # 还需要让标签有bootstrap样式
                                       widget=forms.widgets.PasswordInput(attrs={'class': 'form-control'})
                                       )
    email = forms.EmailField(label='邮箱',
                             error_messages={
                                 'required': '邮箱不能为空',
                                 'invalid': '邮箱格式不正确',
                             },
                             # 还需要让标签有bootstrap样式
                             widget=forms.widgets.EmailInput(attrs={'class': 'form-control'})
                             )

    # 钩子函数
    # 局部钩子：校验用户名是否已存在
    def clean_username(self):
        username = self.cleaned_data.get('username')
        # 去数据库校验
        is_exist = models.UserInfo.objects.filter(username=username)
        if is_exist:
            # 提示信息
            self.add_error('username', '用户已存在')
        return username

    # 全局钩子：校验两次是否一致
    def clean(self):
        password = self.cleaned_data.get('password')
        confirm_password = self.cleaned_data.get('confirm_password')
        if not password == confirm_password:
            self.add_error('confirm_password', '两次密码不一致')

        return self.cleaned_data

class BootstrapBaseFrom(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        #给实例对象的每一个字段添加class属性
        # print(self.fields)
        for field in self.fields.values():
            field.widget.attrs.update({'class':'form-control'})


class OrderForm(BootstrapBaseFrom):
    class Meta:
        model = models.Order
        exclude = ['is_delete','is_plan','is_production','item_date']




class PlanTableForm(BootstrapBaseFrom):
    class Meta:
        model = models.PlanTable
        exclude = ['change_time','order','work_process','customer','plan_code']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['sku'].choices = [(self.instance.sku.id,self.instance.sku.name)]

    # def __init__(self, *args, **kwargs):
    #     super().__init__(*args, **kwargs)
        # self.fields['customer'].choices = [(self.instance.customer.id,self.instance.customer.small_name)]
        #根据选择的订单编号，设置计划的客户信息，机种信息
        # print(self.instance.code,type(self.instance.code))
        # self.fields['order'].choices = [(self.instance.id,self.instance.code)]
        # self.fields['customer'].choices = ''
        # plan_obj = models.PlanTable.objects.filter(order_id=self.instance.id).first()
        # self.fields['customer'].choices = forms.models.ModelChoiceField(queryset=models.Order.objects.filter(id=plan_obj.order).values_list('id','customer'))
        # self.fields['plan_code'].choices = forms.models.ModelChoiceField(queryset=models.Order.objects.filter(id=self.instance.id))
        # self.fields['sku'].choices = [(self.instance.sku.id,self.instance.sku.name)]
        # self.fields['line'].choices = models.choices_line
        # # self.fields['Lot'].choices = ((self.instance.data_number,))
        # self.fields['work_process'].choices = models.choices_pro

class SequencePlanTableForm(BootstrapBaseFrom):
    class Meta:
        model = models.PlanTable
        fields = ['plan_pro_code','sku','production_line_num','work_num','date_num','production_department','line']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['sku'].choices = [(self.instance.sku.id,self.instance.sku.name)]


class DailyProductionForm(BootstrapBaseFrom):
    class Meta:
        model = models.DailyProduction
        fields = '__all__'


class STForm(BootstrapBaseFrom):
    class Meta:
        model = models.ST
        fields = '__all__'






